package com.hisense.ovcloud.apicontrol.config;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;

import java.io.IOException;
import java.util.LinkedHashMap;

public class LinkedHashMapSerializer extends JsonSerializer<LinkedHashMap> {
    @Override
    public void serialize(LinkedHashMap linkedHashMap, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException {
        jsonGenerator.writeStartObject();
        linkedHashMap.forEach((k, v) -> {
            try {
                jsonGenerator.writeFieldName(k.toString());
                serializerProvider.defaultSerializeValue(v, jsonGenerator);
            } catch (Exception e) {
                throw new RuntimeException("字段序列化失败", e);
            }
        });
        jsonGenerator.writeEndObject();
    }
}
